package com.situ.yB2C.catalog.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.situ.yB2C.catalog.pojo.Catalog;
import com.situ.yB2C.catalog.service.CatalogService;
import com.situ.yB2C.user.controller.UserController;
import com.situ.yB2C.user.pojo.User;

@Controller
@RequestMapping("catalog/")
public class CatalogController {
	private static final Logger LOG = LoggerFactory.getLogger(UserController.class);
	@Autowired
	private CatalogService catalogService;
	
	
	@RequestMapping("index")
	public ModelAndView goindex(ModelAndView modelAndView) {
		modelAndView.setViewName("catalog/index");
		return modelAndView;
	}
	//新增一级目录
	@ResponseBody
	@RequestMapping("goadd")
	public ModelAndView goAdd(Long parentId , ModelAndView modelAndView) {
//		String parentName = null;
//		if(parentId==-1) {
//			parentName = "最上级";
//		}else {
//			parentName = catalogService.getNameById(parentId);
//		}
	    modelAndView.addObject("parentId", -1);
	    System.out.println(parentId);
		modelAndView.addObject("parentName", "一级目录");
		modelAndView.setViewName("catalog/form");
		return modelAndView;
	}
	//新增子目录
	@ResponseBody
	@RequestMapping("goaddchild")
	public ModelAndView doadd(Long parentId , ModelAndView modelAndView) {
		modelAndView.addObject("parentId", parentId);
		modelAndView.addObject("parentName", catalogService.findOneById(parentId).getCataName());
		modelAndView.setViewName("catalog/form");
		return modelAndView;
	}
	//zhixing
	
	@ResponseBody
	@RequestMapping("doadd")
	public int doAddCatalog(Catalog catalog) {
		
		return catalogService.saveCatalog(catalog);
	}

	@RequestMapping("list")
	public ModelAndView findAllCatalogList(ModelAndView modelAndView){
		List<Catalog> cataList = catalogService.findAllCatalog();
		modelAndView.addObject("catalogList", cataList);
		modelAndView.setViewName("catalog/list");
		return modelAndView;
	}
//	@ResponseBody
//	@RequestMapping("list")
//	public List<Catalog>findAllCatalogList(){
//		
//		return catalogService.findAllCatalog();
//	}
	
//	//执行新增
//	@ResponseBody
//	@RequestMapping("goaddchild")
//	public int doAdd (Catalog catalog) {
//		
//		return catalogService.saveCatalog(catalog);
//	}
	//删除
	@ResponseBody
	@RequestMapping("dodelete")
	public int deleteCataById(Long rowId) {
		return catalogService.deleteCatalogById(rowId);
	}
	//修改
	@ResponseBody
	@RequestMapping("goedit")
	public ModelAndView goEdit(Long rowId,ModelAndView modelAndView) {
		Catalog catalog = catalogService.findOneById(rowId);
		modelAndView.addObject("catalog",catalog);
		modelAndView.setViewName("catalog/list");
		return modelAndView;
	}
	//执行修改
	@ResponseBody
	@RequestMapping("doedit")
	public int doEdit(Catalog catalog) {
		return catalogService.updateByCatalogInof(catalog);
	}
}
